/*
 *  setoccupier.h
 *  UnixSysPro
 *
 *  Created by ? ? on 07-8-24.
 *  Copyright 2007 __MyCompanyName__. All rights reserved.
 *
 */

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

void setoccupier(int roomno,char* namebuf,int bufsize)
{
	int outfile = -1;
	off_t offset;
	ssize_t nwrite;
	
	// open the file first time around
	if(outfile == -1 &&
	   (outfile = open("residents",O_WRONLY|O_CREAT|O_TRUNC,0644)) == -1)
	{
		printf("can not create file residents! \n");
		return;		
	}
	
	offset = (roomno -1)*bufsize;
	
	if((offset = lseek(outfile,offset,SEEK_SET)) == -1)
	{
		printf("can not lseek the file\n");
		close(outfile);
		return;
	}
	
	if((nwrite = write(outfile,namebuf,bufsize)) < bufsize)
	{
		print("something wrong\n");
		close(outfile);
		return;
	}
	
	close(outfile);
	
	printf("setoccupoer successful\n");
	return;
}